package send.news;

/**
 * Created by shugen on 2017/3/11.
 */


import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import send.model.News;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class jsoupUtil {

    public static void main(String[] args) throws IOException {

        StringBuffer buffer = new StringBuffer();
        buffer.append("http://news.baidu.com/ns?cl=2&rn=10&tn=news&word=福建农林大学");
        Document document = Jsoup.connect(buffer.toString()).userAgent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Firefox/47.0").ignoreContentType(true).get();
        Elements elements = document.select("[class=result]");
        for (Element element : elements) {
            Elements titleElement = element.select("a[href]");
            String link = titleElement.attr("href").trim();
            Elements elements2 = element.select("a");
            String title = elements2.get(0).text();
            Elements elements4 = element.select("[class=c-author]");
            String autherTest = elements4.get(0).text();
            String iii = element.html().replace("</em>","");
            int beginIndex = iii.lastIndexOf("<em>") + 4;
            int endIndex = iii.indexOf("<span class=\"c-info\">");

//	String memo = elements5.get(0).text();
//String[] strArray =  autherTest.split("&nbsp;$nbsp;| ");
//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//	Date date = format .parse(strArray[1]);
String detail = element.select(".detail").text();

            System.out.println("url： " + link);
            System.out.println("标题： " +title);
            System.out.println("作者： " + autherTest);
            System.out.println("简介："+iii.substring(beginIndex, endIndex));


        }

        System.out.println(elements.size());

    }

    public static List<News> getnewsnow() throws IOException {
        StringBuffer buffer = new StringBuffer();
        List<News> list =new ArrayList<News>();
        try {
            buffer.append("http://news.baidu.com/ns?cl=2&rn=12&tn=news&word=福建农林大学");
            Document document = Jsoup.connect(buffer.toString()).userAgent("Mozilla/5.0 (Windows NT 6.1; rv:30.0) Firefox/47.0").ignoreContentType(true).get();
            Elements elements = document.select("[class=result]");
            int i = 1;
            for (Element element : elements) {
                Elements titleElement = element.select("a[href]");
                String link = titleElement.attr("href").trim();
                Elements elements2 = element.select("a");
                String title = elements2.get(0).text();
                String iii = element.html().replace("</em>", "");
                int beginIndex = iii.lastIndexOf("<em>") + 4;
                int endIndex = iii.indexOf("<span class=\"c-info\">");
                list.add(new News(i++, title, null, iii.substring(beginIndex, endIndex), link, ""));

            }
        }
        catch (Exception e){
            System.out.println("获取新闻失败,请检查网络!");
            return list;
        }
        return list;

    }

}